<template>
  <div>
    <div class="search">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="供应商名称">
          <el-input v-model="formData.name" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="供应商联系人">
          <el-input v-model="formData.contact" placeholder="请输入供应商联系人" />
        </el-form-item>
        <el-form-item label="供应商联系方式">
          <el-input v-model="formData.phone" placeholder="请输入供应商联系方式" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getdata">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="dialogVisible = true">添加</el-button>
        </el-form-item>
      </el-form>

    </div>

    <div>
      <el-table :data="tableData" border>
        <el-table-column prop="id" label="供应商编码" />
        <el-table-column prop="name" label="供应商名称" />
        <el-table-column prop="settlement" label="采购期限" />
        <el-table-column prop="address" label="供应商地址" />
        <el-table-column prop="contact" label="联系人姓名" />
        <el-table-column prop="phone" label="联系人电话" />
        <el-table-column prop="operate" label="操作">
          <template slot-scope="scope">
            <el-button size="small" type="danger" @click="remove(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        :page-size="limit"
        :total="total"
        style="padding:30px 0;text-align:center;"
        layout="total,prev,pager,next,jumper"
        @current-change="getPagedata"
      />
    </div>

    <el-dialog
      title="添加供应商"
      :visible.sync="dialogVisible"
      width="32%"
      :before-close="handleClose"
    >

      <el-form ref="supplierInfo" :model="supplierInfo" label-position="right" :rules="rules" label-width="100px" label-suffix=":">
        <el-form-item label="供应商名称" prop="name">
          <el-input v-model="supplierInfo.name" placeholder="请输入供应商名称" style="width: 300px" />
        </el-form-item>
        <el-form-item label="采购期限" prop="settlement">
          <el-input v-model="supplierInfo.settlement" placeholder="请输入采购期限" style="width: 300px" />
        </el-form-item>
        <el-form-item label="供应商地址" prop="address">
          <el-input v-model="supplierInfo.address" placeholder="请输入供应商地址" style="width: 300px" />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="supplierInfo.contact" placeholder="请输入供应商联系人" style="width: 300px" />
        </el-form-item>

        <el-form-item label="联系人电话" prop="phone">
          <el-input v-model="supplierInfo.phone" placeholder="请输入联系人电话" style="width: 300px" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSupplier('supplierInfo')">添加</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import supplierApi from '@/api/supplier/supplier'

export default {
  data() {
    return {
      formData: {

      },
      tableData: null,
      current: 1, // 当前页
      limit: 1, // 每页显示记录数
      total: 0, // 总记录数
      dialogVisible: false,
      supplierInfo: {
        name: '',
        contact: '',
        phone: '',
        settlement: '',
        address: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入供应商名称', trigger: 'blur' }
        ],
        contact: [
          { required: true, message: '请输入联系人', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入联系人电话', trigger: 'blur' }
        ],
        settlement: [
          { required: true, message: '请输入采购期限', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入供应商地址', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getdata()
  },
  methods: {
    remove(id) {
      this.$confirm('此操作将永久删除,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => { // 确定执行then方法
        // 调用接口
        console.log(id)
      })
    },
    // 获取数据
    getdata() {
      this.formData.page = this.current
      this.formData.limit = this.limit
      console.log(this.formData)
      supplierApi.allBySupplier(this.formData).then(
        res => {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      )
    },
    getPagedata(page = 1) {
      this.current = page
      this.formData.page = this.current
      this.formData.limit = this.limit
      supplierApi.allBySupplier(this.formData).then(
        res => {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      )
    },
    addSupplier(supplierInfo) {
      // eslint-disable-next-line no-undef
      this.$refs[supplierInfo].validate((valid) => {
        if (valid) {
          // 添加商品
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.$message('取消操作')
          done()
        })
        .catch(_ => {})
    }
  }
}
</script>
<style>

</style>
